//--------------------Sonic CD UFO Script---------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
private alias object.ypos : Object.ZPos

private alias object.value0 : Object.TargetNode
private alias object.value1 : Object.XVelocity
private alias object.value2 : Object.ZVelocity
private alias object.value3 : Object.Timer
private alias object.value4 : Object.ScreenXPos // Difference from the center of the camera (which is where Sonic is always located)
private alias object.value5 : Object.ScreenDepth

// HUD Alias
private alias object.value0 : HUD.UFOsCount // Initially set via a loop in the UFO script
private alias object.value1 : HUD.TimeSeconds // Displayed time
private alias object.value3 : HUD.Rings
private alias object.value4 : HUD.LastUFOType // Used by UFOs for ring streak bonus
private alias object.value5 : HUD.SpeedShoesTimer // Used to transmit between player and UFO
private alias object.value6 : HUD.BonusUFONo // "No" akin to Entity No, not as in "is there no UFO?"

public alias  256 		: GROUP_PLAYERS
public alias  arrayPos6 : currentPlayer

// UFOPowerUp Aliases
private alias object.value0 : UFOPowerUp.ExplosionDir
private alias object.value1 : UFOPowerUp.ExplosionXVel
private alias object.value2 : UFOPowerUp.ExplosionYVel
private alias object.value4 : UFOPowerUp.YVelocity
private alias object.value5 : UFOPowerUp.ScreenDepth
private alias object.value7 : UFOPowerUp.StartResults

// SFX Aliases
private alias 1 : SFX_G_RING

// Property Value
private alias 0 : UFO_CONTENTS_RINGS
private alias 1 : UFO_CONTENTS_SPEEDDHOES
private alias 2 : UFO_CONTENTS_TIME
private alias 3 : UFO_CONTENTS_CHAODRIVE

// Priority
private alias 1 : PRIORITY_ACTIVE



event ObjectMain
	// Update movement
	if Object.Timer == 0
		arrayPos0 = Object.TargetNode
		if object[arrayPos0].type != TypeName[UFO Node]
			Object.TargetNode = object.entityPos
			Object.TargetNode++
			arrayPos0 = Object.TargetNode
		end if

		object.xpos = object[arrayPos0].xpos
		Object.ZPos = Object.ZPos[arrayPos0]
		Object.Timer = object[arrayPos0].propertyValue
		Object.Timer *= 2

		arrayPos0++
		Object.TargetNode++
		if object[arrayPos0].type != TypeName[UFO Node]
			Object.TargetNode = object.entityPos
			Object.TargetNode++
			arrayPos0 = Object.TargetNode
		end if

		Object.XVelocity = object[arrayPos0].xpos
		Object.XVelocity -= object.xpos
		Object.XVelocity /= Object.Timer

		Object.ZVelocity = Object.ZPos[arrayPos0]
		Object.ZVelocity -= Object.ZPos
		Object.ZVelocity /= Object.Timer
	else
		// Following a node - just update movement for now

		Object.Timer--
		object.xpos += Object.XVelocity
		Object.ZPos += Object.ZVelocity
	end if

	// Check player interaction in this (rather lengthy) process

	// X difference
	temp0 = object.xpos
	temp0 -= tileLayer[0].xpos
	temp0 >>= 8

	// Z mutliplier
	temp1 = Object.ZPos
	temp1 -= tileLayer[0].zpos
	temp1 >>= 8

	// Calculating the X difference
	Sin(temp2, tileLayer[0].angle)
	temp2 *= temp1
	Cos(temp3, tileLayer[0].angle)
	temp3 *= temp0
	Object.ScreenXPos = temp2
	Object.ScreenXPos -= temp3
	Object.ScreenXPos >>= 9

	// Calculating the Z difference
	Cos(temp2, tileLayer[0].angle)
	temp2 *= temp1
	Sin(temp3, tileLayer[0].angle)
	temp3 *= temp0
	Object.ScreenDepth = temp2
	Object.ScreenDepth += temp3
	Object.ScreenDepth >>= 9

	// Long list of checks for the hitbox detection

	// The Player has to be more than 54 pixels off the ground, but lower than 122 pixels towards the sky
	foreach (TypeName[Sonic], arrayPos2, ALL_ENTITIES)
		if object[arrayPos2].ypos > 0x360000
			if object[arrayPos2].ypos < 0x7A0000

				// The UFO has to be within the 2100 range from the player, who's at a depth of 0x5800 normally
				if Object.ScreenDepth < 0x58C8
					if Object.ScreenDepth > 0x5094

						// The UFO has to be between -0.125 and +0.125 pixels away from the screen centre
						if Object.ScreenXPos > -0x2000
							if Object.ScreenXPos < 0x2000

								// All those checks passed, let's destroy this UFO!

								temp0 = Object.ScreenXPos
								temp0 *= 192
								temp0 /= Object.ScreenDepth
								temp0 += screen.xcenter
								temp0 <<= 16

								temp1 = tileLayer[0].ypos
								temp1 >>= 8
								temp1 -= 0x6000
								temp1 *= 96
								temp1 /= Object.ScreenDepth
								temp1 += 128
								temp1 <<= 16

								// Play the ring SFX
								PlaySfx(SFXName[Ring L], false)

	#platform: Use_Haptics
								// Do shake index 55 to really sell the impact of hitting a floating UFO
	// WARNING: Function FUNC_HAPTICEFFECT does not exist in RSDKv4!
	// Arguments: 55, 0, 0, 0

	#endplatform

								switch object.propertyValue
								case UFO_CONTENTS_RINGS
									// Decrease the UFO count and give some bonus rings
									HUD.UFOsCount[4]--
									if HUD.LastUFOType[4] == object.propertyValue
										// If the last UFO caught was a rings variant too, then double the player's rings rather than adding 20
										HUD.Rings[4] <<= 1
									else
										HUD.Rings[4] += 20
									end if
									break

								case UFO_CONTENTS_SPEEDDHOES
									// Decrease the UFO count and give the player some new faster shoes
									HUD.UFOsCount[4]--
									HUD.SpeedShoesTimer[4] = 200
									break

								case UFO_CONTENTS_TIME
									// Aadd some time to the clock
									// (Not decreasing the UFO count because time UFOs don't count towards the whole)
									HUD.TimeSeconds[4] += 30
									break

								case UFO_CONTENTS_CHAODRIVE
									// Decrease the UFO count and give a chao drive
									HUD.UFOsCount[4]--
									break
								end switch

								HUD.LastUFOType[4] = object.propertyValue

								// Create the UFO powerup icon
								CreateTempObject(TypeName[UFO PowerUp], object.propertyValue, temp0, temp1)
								Rand(UFOPowerUp.ExplosionDir[TempObjectPos], 100)
								UFOPowerUp.ExplosionXVel[TempObjectPos] = temp0
								UFOPowerUp.ExplosionYVel[TempObjectPos] = temp1
								UFOPowerUp.YVelocity[TempObjectPos] = -0x40000
								UFOPowerUp.ScreenDepth[TempObjectPos] = 0x58C8

								// If the last UFO was just destroyed, then signal that to the PowerUp object too
								if HUD.UFOsCount[4] == 0
									stage.timeEnabled = false
									UFOPowerUp.StartResults[TempObjectPos] = true
								end if

								// Blank this UFO object since it's been 
								ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)

								// And now, close a whole lotta end if's...

							end if
						end if
					end if
				end if
			end if
		end if
	next
end event


event ObjectDraw
	if Object.ScreenDepth > 256 // Don't draw if the UFO is too close to the screen

		// Do some maths to find the position & scale the UFO should be at

		object.scale = 0xC00000
		object.scale /= Object.ScreenDepth

		temp0 = 768
		temp0 -= object.scale
		temp0 /= 6

		temp1 = Object.ScreenDepth
		temp1 >>= 12

		if temp0 > 0
			object.scale += temp0
			object.scale -= temp1
		end if

		temp0 = Object.ScreenXPos
		temp0 *= 192
		temp0 /= Object.ScreenDepth
		temp0 += screen.xcenter

		temp1 = tileLayer[0].ypos
		temp1 >>= 8
		temp1 -= 0x6000
		temp1 *= 96
		temp1 /= Object.ScreenDepth
		temp1 += 128

		// Draw the UFO now
		DrawSpriteScreenFX(object.propertyValue, FX_SCALE, temp0, temp1)

		// Find the ground position
		temp1 = tileLayer[0].ypos
		temp1 >>= 8
		temp1 *= 96
		temp1 /= Object.ScreenDepth
		temp1 += 128

		// Draw the shadow on the ground
		DrawSpriteScreenFX(4, FX_SCALE, temp0, temp1)

	end if

end event


event ObjectStartup

	LoadSpriteSheet("Special/Objects.gif")

	// UFO Frames

	// 0 - 10 Rings Variant
	SpriteFrame(-40, -28, 80, 56, 1, 1)

	// 1 - Speed Shoes Variant
	SpriteFrame(-40, -28, 80, 56, 82, 1)

	// 2 - Extra Time Variant
	SpriteFrame(-40, -28, 80, 56, 163, 1)

	// 3 - Chao Drive Variant
	SpriteFrame(-40, -28, 80, 56, 244, 1)

	// 4 - UFO Shadow Frame
	SpriteFrame(-40, -8, 80, 16, 1, 117)

	// Cycle through all UFO objects in the scene and set them up
	arrayPos0 = 32
	while arrayPos0 < 1056
		if object[arrayPos0].type == TypeName[UFO]

			// Set the UFO to always be active
			object[arrayPos0].priority = PRIORITY_ACTIVE

			if object[arrayPos0].propertyValue != 2

				// Increase the UFO count
				HUD.UFOsCount[4]++

			else

				// Bonus UFO Spawn point, delete the UFO for right now and store its EntityPos into the HUD value
				// (We need to make sure the Time UFO is spawned in the correct slot because of its Nodes)

				object[arrayPos0].type = TypeName[Blank Object]
				HUD.BonusUFONo[4] = arrayPos0

			end if
		end if

		arrayPos0++
	loop

end event


// ========================
// Editor Subs
// ========================




// Property Value


// Contents





// Property Value


// Contents







event RSDKDraw
	DrawSprite(object.propertyValue)

	if Editor.ShowGizmos == true
		// Draw arrows to show the network of UFO nodes

		Editor.DrawingOverlay = true

		arrayPos1 = object.entityPos
		arrayPos0 = object.entityPos
		arrayPos0++

		// TODO: change this to "UFO Node" once GetObjectType works with spaces


		while object[arrayPos0].type == temp0

			arrayPos0++
			arrayPos1++
		loop

		arrayPos0 = object.entityPos
		arrayPos0++
		if arrayPos1 != object.entityPos

		end if

		Editor.DrawingOverlay = false
	end if
end event


event RSDKLoad
	LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(-40, -28, 80, 56, 1, 1)
	SpriteFrame(-40, -28, 80, 56, 82, 1)
	SpriteFrame(-40, -28, 80, 56, 163, 1)






end event
